package com.gilight.base.mq.handler;

import com.gilight.base.admin.order.AdminOrderService;
import com.gilight.base.base.exception.AppServiceException;
import com.gilight.base.base.exception.ExceptionDefinition;
import com.gilight.base.component.LockComponent;
import com.gilight.base.component.LockConst;
import com.gilight.base.enums.DMQHandlerType;
import com.gilight.base.enums.OrderStatusType;
import com.gilight.base.model.order.OrderDO;
import com.gilight.base.service.order.OrderBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

public class OrderAutoShipHandle implements RedisNotifyHandler {

    private static final Logger logger = LoggerFactory.getLogger(OrderAutoShipHandle.class);
    @Autowired
    private AdminOrderService adminOrderService;
    @Autowired
    private OrderBizService orderBizService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int handle(String orderNo) {
        try {
            OrderDO orderDO = orderBizService.checkOrderExistByNo(orderNo, null).get(0);
            if (orderDO.getStatus() != OrderStatusType.WAIT_STOCK.getCode()) {
                throw new AppServiceException(ExceptionDefinition.ORDER_STATUS_NOT_SUPPORT_SHIP);
            }
            adminOrderService.ship(orderNo,null,null,null,null);
            logger.info("[订单自动制作完成] 订单号：" + orderNo);
        } catch (Exception e) {
            logger.error("[订单自动制作完成] 异常", e);
            return 0;
        }
        return 1;
    }

    @Override
    public int getCode() {
        return DMQHandlerType.ORDER_AUTO_SHIP.getCode();
    }
}
